/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author tea milandi
 */
public class PenerimaanBarang {

    private String kodePenerimaan;
    private String waktuPenerimaan;
    private double jumlahBarang;
    private double hargaBarang;
    private String idBarang;
    private String username;
    String jdbcUrl = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
    String userid = "mhs115314008"; // gantikan dengan username anda
    String pass = "mhs115314008"; // gantikan dengan password anda

    /**
     * @return the kodePenerimaan
     */
    public void setKodePenerimaan(String kode) {
        this.kodePenerimaan = kode;
    }

    public String getKodePenerimaan() throws SQLException {
        Connection connection = null;
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        ds.setPassword(pass);
        ds.setUser(userid);
        connection = (Connection) ds.getConnection();
        Statement statement = connection.createStatement();
        SimpleDateFormat sdf1 = new SimpleDateFormat("dMyy");
        Date d = new Date();
        String finalDate = sdf1.format(d);
        String k1 = generateKodePenerimaan(finalDate);
        String code = null;
        String kod = k1.substring(0, 6);
        int a = 0;
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery("select kodepenerimaan from penerimaan_asmart where kodepenerimaan like '" + kod + "%'");
        while (rs.next()) {
            String sementara = rs.getString("kodepenerimaan");
            String s2 = sementara.substring(6);
            a = Integer.parseInt(s2);
            a++;
        }
        return this.kodePenerimaan = kod + String.valueOf(a);
    }

    /**
     * @return the waktuPenerimaan
     */
    public String getWaktuPenerimaan() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        this.waktuPenerimaan = simpleDateFormat.format(date);
        return waktuPenerimaan;
    }

    /**
     * @return the jumlahBarang
     */
    public double getJumlahBarang() {
        return jumlahBarang;
    }

    /**
     * @param jumlahBarang the jumlahBarang to set
     */
    public void setJumlahBarang(double jumlahBarang) {
        this.jumlahBarang = jumlahBarang;
    }

    /**
     * @return the hargaBarang
     */
    public double getHargaBarang() {
        return hargaBarang;
    }

    /**
     * @param hargaBarang the hargaBarang to set
     */
    public void setHargaBarang(double hargaBarang) {
        this.hargaBarang = hargaBarang;
    }

    /**
     * @return the idBarang
     */
    public String getIdBarang() {
        return idBarang;
    }

    /**
     * @param idBarang the idBarang to set
     */
    public void setIdBarang(String idBarang) {
        this.idBarang = idBarang;
    }

    /**
     * @return the username
     */
    public void insertPenerimaan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeQuery("insert into penerimaan_asmart values ('"
                    + getKodePenerimaan() + "','"
                    + getWaktuPenerimaan() + "','"
                    + getJumlahBarang() + "','"
                    + getHargaBarang() + "','"
                    + getIdBarang() + "','"
                    + getUsername() + "')");
            StokBarang barang = new StokBarang();
            barang.setHargaBarang(getHargaBarang());
            barang.setIdBarang(getIdBarang());
            barang.setJumlahBarang(getJumlahBarang());
            barang.tambahBarang();
            barang.updateHargaBarangGudang();
        } catch (SQLException e) {
            System.out.println("error :" + e.getMessage());
        }
    }

    public String generateKodePenerimaan(String date) {
        String kode;
        kode = date + "0";
        return kode;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
}
